package com.fantasy.dao.impl;

import com.fantasy.dao.BaseDao;
import com.fantasy.dao.VideoDao;
import com.fantasy.entity.Video;

import java.util.List;

public class VideoDaoImpl extends BaseDao implements VideoDao {

    
    @Override
    public int insert(Video video) {
        String sql = "INSERT INTO `video`(`name`,`userId`,`typeId`,`describe`,`examine`,`isViewable`,`uploadDate`,`starNum`,`collectionNum`,`url`,`videoImg`,`visit`)VALUE(?,?,?,?,?,?,?,?,?,?,?,?)";
        return super.update(sql,video.getName(),video.getUserId(),video.getTypeId(),video.getDescribe(),video.getExamine(),video.getIsViewable(), video.getUploadDate(),video.getStarNum(),video.getCollectionNum(),video.getUrl(),video.getVideoImg(),video.getVisit());
    }

    @Override
    public int upDate(Video video) {
        String sql = "UPDATE `video` SET `name`=?,`userId`=?,`typeId`=?,`describe`=?,`examine`=?,`isViewable`=?,`uploadDate`=?,`starNum`=?,`collectionNum`=?,`url`=?    ,`videoImg`=?,`visit`=? WHERE `id`=?";
        return super.update(sql,video.getName(),video.getUserId(),video.getTypeId(),video.getDescribe(),video.getExamine(),video.getIsViewable(), video.getUploadDate(),video.getStarNum(),video.getCollectionNum(),video.getUrl(),video.getVideoImg(),video.getVisit(),video.getId());
    }

    @Override
    public int delete(Long videoID) {
        String sql = "DELETE FROM `video` WHERE `id`=?";
        return super.update(sql,videoID);
    }

    @Override
    public List<Video> queryAll() {
        String sql = "SELECT * FROM `video`";
        return super.queryForList(Video.class,sql);
    }

    @Override
    public Video queryById(Long videoID) {
        String sql="SELECT * FROM `video` WHERE `id`=?";
        return super.queryForOne(Video.class,sql,videoID);
    }

    @Override
    public Integer queryForPageTotalCount() {
        String sql="SELECT COUNT(1) FROM `video`";
        Number count = (Number)queryForSingleValue(sql);
        return count.intValue();
    }

    @Override
    public List<Video> queryForPageItems(int begin, int pageSize) {
        String sql="SELECT * FROM `video` LIMIT ?,?";
        return super.queryForList(Video.class,sql,begin,pageSize);
    }

    @Override
    public List<Video> pulley(int begin, int pageSize, int typeId) {
        String sql = "SELECT * FROM `video` WHERE typeId = ? LIMIT ?,?";
        return queryForList(Video.class,sql,typeId,begin,pageSize);
    }

    @Override
    public int videTypeCount(int typeId) {
        String sql = " SELECT * FROM `video` WHERE typeId = ? ";
        return super.queryForList(Video.class, sql, typeId).size();
    }

    @Override
    public Integer queryForResultPageTotalCount(String videoName) {
        String sql="SELECT COUNT(1) FROM `video` WHERE `name`LIKE \"%\"?\"%\"";
        Number count = (Number)queryForSingleValue(sql,videoName);
        return count.intValue();
    }

    @Override
    public List<Video> queryForResultPageItems(String videoName ,int begin, int pageSize) {
        String sql="SELECT * FROM `video` WHERE `name`LIKE \"%\"?\"%\" LIMIT ?,?";
        return super.queryForList(Video.class,sql,videoName,begin,pageSize);
    }

    @Override
    public List<Video> queryByUserId(String userId) {
        String sql = "select * from `video` where userid = ?";
        return queryForList(Video.class,sql,userId);
    }

    @Override
    public Integer queryForUserVideoTotalCount(int userId) {
        String sql="SELECT COUNT(1) FROM `video` WHERE `userId`=?";
        Number count = (Number) queryForSingleValue(sql, userId);
        return count.intValue();
    }
}
